package cn.tedu.charging.device.service.impl;


import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.DeviceRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.device.pojo.po.ChargingPileInfoPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import cn.tedu.charging.device.service.DeviceService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceRepository deviceRepository;
    @Autowired
            @Qualifier("GEOANDString")
    StationCacheRepository stationCacheRepository;

    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        log.debug("获取站场信息：{}",gunId);


        ChargingGunInfoPO chargingGunInfoPO =deviceRepository.selectFromGunInfo(gunId);
        ChargingPileInfoPO chargingPileInfoPO = deviceRepository.selectFromPile(chargingGunInfoPO.getPileId());
        StationPO stationPO = deviceRepository.selectFromStation(chargingPileInfoPO.getStationId());
        log.debug("获取枪的数据：{}",chargingGunInfoPO);
        log.debug("获取桩的数据：{}",chargingPileInfoPO);
        log.debug("获取站的数据：{}", stationPO);



        StationInfoVO stationInfoVO = new StationInfoVO();
        stationInfoVO.setId(stationPO.getId());
        stationInfoVO.setOperatorId(stationPO.getOperatorId());


        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        ChargingGunInfoPO chargingGunInfoPO = new ChargingGunInfoPO();
//
        chargingGunInfoPO.setId(gunStatusUpdateParam.getGunId());
        chargingGunInfoPO.setGunStatus(gunStatusUpdateParam.getStatus());
        chargingGunInfoPO.setUpdateTime(new Date());
        return deviceRepository.updateGunStatus(chargingGunInfoPO);

    }

    @Override
    public List<StationInfoVO> getNearStation(Double longitude, Double latitude, Double distance) {
        List<StationPO> stationPOS = stationCacheRepository.search(longitude, latitude, distance);
        List<StationInfoVO> stationInfoVOS=new ArrayList<>();

        for (StationPO stationPO : stationPOS){
            StationInfoVO stationInfoVO=new StationInfoVO();
            BeanUtils.copyProperties(stationPO,stationInfoVO);
            stationInfoVOS.add(stationInfoVO);

        }
        return stationInfoVOS;
    }
}
